trigger primaryContact on Contact (before insert, before update)
{
    ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeID('Contact', 'Local Advertiser');
    Map<id,contact> NewPrimaryContactIDs =  new Map<id,contact>();
    Map<id,contact> NewPrimaryContactIDs_api =  new Map<id,contact>();
    Map<id,string> PrimaryContacts =  new Map<id,string>();
    Map<id,string> PrimaryPhones =  new Map<id,string>();
    Map<id,string> CleanPrimaryPhonetext=  new Map<id,string>();
    List<Id> NonPrimaryAccIds =  new List<Id>();
    List<Contact> ExistingPrimaryContacts =  new List<Contact>();
    List<Contact> ExistingPrimaryContacts_api =  new List<Contact>();
    List<Contact> unCheckPrimary  =  new List<Contact>();  
    List<Account> Acclist =  new List<Account>(); 
    Map<id,COntact> oldObj = Trigger.oldMap;  

    
        for(Contact newContact:Trigger.new){
            if(newContact.RecordTypeId==localAdvertiserRecordTypeID && ((trigger.isInsert && newContact.Primary_Indicator__c==true && newContact.api_controlled__c == false) || ((trigger.isUpdate && newContact.api_controlled__c == false && newContact.Primary_Indicator__c ==true) && (oldObj.get(newContact.id).Primary_Indicator__c==false || oldObj.get(newContact.id).phone!=newContact.phone || oldObj.get(newContact.id).FirstName!=newContact.FirstName || oldObj.get(newContact.id).LastName!=newContact.LastName)))){
                    //CleanPhoneText = SilCustomerHelper.CreatePhoneNumberHash(newContact.Phone);
                    NewPrimaryContactIDs.put(newContact.Accountid,newContact);
                    //string cName = newContact.FirstName + ' ' +newContact.LastName;
                    PrimaryContacts.put(newContact.Accountid,newContact.FirstName + ' ' +newContact.LastName);
                    PrimaryPhones.put(newContact.Accountid,newContact.Phone);
                    //CleanPrimaryPhonetext.put(newContact.Accountid,CleanPhoneText);
              }
              else if (newContact.RecordTypeId==localAdvertiserRecordTypeID && ((trigger.isInsert && newContact.Primary_Indicator__c==true && newContact.api_controlled__c == true) || ((trigger.isUpdate && newContact.Primary_Indicator__c == true && newContact.api_controlled__c == true) && (oldObj.get(newContact.id).Primary_Indicator__c==false || oldObj.get(newContact.id).phone!=newContact.phone || oldObj.get(newContact.id).FirstName !=newContact.FirstName || oldObj.get(newContact.id).LastName!=newContact.LastName)))){                  
                    //CleanPhoneText = SilCustomerHelper.CreatePhoneNumberHash(newContact.Phone);
                    NewPrimaryContactIDs_api.put(newContact.Accountid,newContact);
                    //string cName_api = newContact.FirstName + ' ' +newContact.LastName;
                    PrimaryContacts.put(newContact.Accountid,newContact.FirstName + ' ' +newContact.LastName);
                    PrimaryPhones.put(newContact.Accountid,newContact.Phone);
                    //CleanPrimaryPhonetext.put(newContact.Accountid,CleanPhoneText);
              }
              }
                     
         if(trigger.isInsert){
             ExistingPrimaryContacts = [Select Id,Name,AccountId,Primary_Indicator__c from Contact where AccountId in :NewPrimaryContactIDs.keyset() and Primary_Indicator__c=true ];
             ExistingPrimaryContacts_api = [Select Id,Name,AccountId,Primary_Indicator__c from Contact where AccountId in :NewPrimaryContactIDs_api.keyset() and Primary_Indicator__c=true ];
         }
         else if (trigger.isUPdate){ 
             ExistingPrimaryContacts = [Select Id,Name,AccountId,Primary_Indicator__c from Contact where AccountId in :NewPrimaryContactIDs.keyset() and id not in :trigger.newmap.keyset() and Primary_Indicator__c=true ];
             ExistingPrimaryContacts_api = [Select Id,Name,AccountId,Primary_Indicator__c from Contact where AccountId in :NewPrimaryContactIDs_api.keyset() and id not in :trigger.newmap.keyset() and Primary_Indicator__c=true ];
            }
         
             for(Contact Cont : ExistingPrimaryContacts ){ 
                        Contact ErrorContact = NewPrimaryContactIDs.get(Cont.Accountid);
                        ErrorContact.Primary_Indicator__c.addError('There can only be a single Primary Contact on Account');
                        NonPrimaryAccIds.add(cont.AccountId);
               }
             for(Contact Cont : ExistingPrimaryContacts_api){ 
                    Cont.Primary_Indicator__c = false;
                    unCheckPrimary.add(Cont);
              }

              if(unCheckPrimary.size() > 0){update unCheckPrimary;}
         
          for(Account Acc : [select id,Phone,name,CleanPrimaryPhone_text__c, primary_phone__c, primary_contact__c from account where (id in :NewPrimaryContactIDs.keyset() or id in :NewPrimaryContactIDs_api.keyset()) and id NOT IN :NonPrimaryAccIds]){
             Acc.primary_phone__c =  PrimaryPhones.get(Acc.id);
             Acc.primary_contact__c =  PrimaryContacts.get(Acc.id);
             Acc.CleanPrimaryPhone_text__c = SilCustomerHelper.CreatePhoneNumberHash(PrimaryPhones.get(Acc.id));//CleanPrimaryPhonetext.get(Acc.id);

         /* Phone populated if null---Sunitha */
         if(Acc.Phone=='' || Acc.Phone==null)
         {
         Acc.Phone=PrimaryPhones.get(Acc.id);
          }
             Acclist.add(acc);}
             
             if(Acclist.size() > 0){Update Acclist;}
          
         }